package edu.ynu.se.xiecheng.achitectureclass.controller;

import edu.ynu.se.xiecheng.achitectureclass.common.controller.LogicController;
import edu.ynu.se.xiecheng.achitectureclass.dao.GreenhouseDao;
import edu.ynu.se.xiecheng.achitectureclass.entity.Greenhouse;
import edu.ynu.se.xiecheng.achitectureclass.service.GreenhouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "大棚管理接口")
@RestController
@RequestMapping("/greenhouse")
public class GreenhouseController extends LogicController<GreenhouseService, GreenhouseDao, Greenhouse, Long> {

    public GreenhouseController(@Autowired GreenhouseService service) {
        super(service);
    }

    @GetMapping("/info")
    @ApiOperation("获取单个大棚信息")
    public ResponseEntity<?> getGreenhouseInfo(@ApiParam("大棚ID") @RequestParam Long id) {
        try {
            Greenhouse greenhouse = getService().getGreenhouseInfo(id);
            
            // 构建返回数据
            Map<String, Object> greenhouseData = new HashMap<>();
            greenhouseData.put("id", greenhouse.getId());
            greenhouseData.put("name", greenhouse.getName());
            greenhouseData.put("address", greenhouse.getAddress());
            greenhouseData.put("area", greenhouse.getArea());
            greenhouseData.put("createTime", greenhouse.getCreateTime());
            greenhouseData.put("updateTime", greenhouse.getUpdateTime());
            
            // 处理设备信息
            if (greenhouse.getDevices() != null) {
                greenhouseData.put("devices", greenhouse.getDevices().stream()
                        .map(device -> {
                            Map<String, Object> deviceData = new HashMap<>();
                            deviceData.put("id", device.getId());
                            deviceData.put("name", device.getName());
                            deviceData.put("deviceType", device.getDeviceType());
                            deviceData.put("serialNumber", device.getSerialNumber());
                            // ... 其他设备字段
                            return deviceData;
                        })
                        .collect(Collectors.toList()));
            }
            
            return ResponseEntity.ok(Map.of(
                    "code", 200,
                    "message", "查询成功",
                    "data", greenhouseData
            ));
        } catch (Exception e) {
            return ResponseEntity.ok(Map.of(
                    "code", 400,
                    "message", e.getMessage()
            ));
        }
    }

    @PostMapping("/update")
    @ApiOperation("更新大棚信息")
    public ResponseEntity<?> updateGreenhouse(@Valid @RequestBody Greenhouse greenhouse) {
        try {
            Greenhouse updatedGreenhouse = getService().updateGreenhouse(greenhouse);
            return ResponseEntity.ok(Map.of(
                    "code", 200,
                    "message", "更新成功",
                    "data", updatedGreenhouse
            ));
        } catch (Exception e) {
            return ResponseEntity.ok(Map.of(
                    "code", 400,
                    "message", e.getMessage()
            ));
        }
    }
} 